<?php
class MemberAction extends Action{
    private $user = null;
    private $nav = null;
    private $address = null;
    private $order = null;
    private $commend = null;

    public function __construct() {
        parent::__construct();
        $this->user = new UserModel();
        $this->nav = new NavModel();
        $this->address = new AddressModel();
        $this->order = new OrderModel();
        $this->commend = new CommendModel();
    }

    public function index() {
        $this->tpl->assign('TenNav',$this->nav->findFrontTenNav());
        $this->tpl->display(SMARTY_FRONT.'public/member.html');
    }

    public function reg() {
        if(isset($_POST['send'])) $this->user->reg() ? $this->redirect->succ('?a=member&m=login','注册成功!') : $this->redirect->error('系统出错，注册失败!');
        if(!isset($_COOKIE['user'])) $this->redirect->error('请先退出当前用户再注册!');
        $this->tpl->assign('TenNav',$this->nav->findFrontTenNav());
        $this->tpl->display(SMARTY_FRONT.'public/reg.html');
    }

    public function login() {
        if(isset($_POST['send'])) $this->user->login() ? $this->redirect->succ('./','登陆成功!') : $this->redirect->error('系统出错，登陆失败!');
        if(isset($_COOKIE['user'])) $this->redirect->error('您已经登陆，请不要重复登陆!');
        $this->tpl->assign('TenNav',$this->nav->findFrontTenNav());
        $this->tpl->display(SMARTY_FRONT.'public/login.html');
    }

    public function alipay() {
        $this->tpl->assign('TenNav',$this->nav->findFrontTenNav());
        $this->tpl->assign('oneOrder',$this->order->findOrderDeatails());
        $this->tpl->display(SMARTY_FRONT.'public/member_alipay.html');
    }

    public function bank() {
        $this->tpl->assign('TenNav',$this->nav->findFrontTenNav());
        $this->tpl->assign('oneOrder',$this->order->findOrderDeatails());
        $this->tpl->display(SMARTY_FRONT.'public/member_bank.html');
    }

    public function intime() {
        $this->tpl->assign('TenNav',$this->nav->findFrontTenNav());
        $this->tpl->assign('oneOrder',$this->order->findOrderDeatails());
        $this->tpl->display(SMARTY_FRONT.'public/member_intime.html');
    }


    public function logout() {
        if($this->user->logout()) $this->redirect->succ('./');
    }

    public function address() {
        if(isset($_POST['send'])) $this->address->add() ? $this->redirect->succ('?a=member&m=address') : $this->redirect->error('系统出错，新增地址失败!');
        $this->tpl->assign('allAddress',$this->address->findAll());
        $this->tpl->assign('TenNav',$this->nav->findFrontTenNav());
        $this->tpl->display(SMARTY_FRONT.'public/member_address.html');
    }

    public function order_details() {
        $this->tpl->assign('TenNav',$this->nav->findFrontTenNav());
        $this->tpl->assign('oneOrder',$this->order->findOrderDeatails());
        $this->tpl->assign('prev',Tool::getPrevPage());
        $this->tpl->display(SMARTY_FRONT.'public/member_order_details.html');
    }

    public function commend() {
        if(isset($_POST['send'])) $this->commend->add() ? $this->redirect->succ(Tool::getPrevPage(),'感谢您的评价!') : $this->redirect->error('系统出错，评价失败!');
        $this->order->isCommend();

        $this->tpl->assign('oneGoods',$this->order->findCommendOrder());
        $this->tpl->assign('oneCommend',$this->commend->findOne());
        $this->tpl->display(SMARTY_FRONT.'public/member_commend.html');
    }

    public function myCommend() {
        parent::page(10,$this->commend);
        $this->tpl->assign('TenNav',$this->nav->findFrontTenNav());
        $this->tpl->assign('myCommend',$this->commend->findMyCommend());
        $this->tpl->display(SMARTY_FRONT.'public/member_mycommend.html');
    }

    public function order() {
        parent::page(10,$this->order);
        $this->tpl->assign('TenNav',$this->nav->findFrontTenNav());
        $this->tpl->assign('AllOrder',$this->order->findUserAllOrder());
        $this->tpl->display(SMARTY_FRONT.'public/member_order.html');
    }

    public function selected() {
        if(isset($_GET['id'])) $this->address->selected() ? $this->redirect->succ(Tool::getPrevPage()) : $this->redirect->error('系统出错，首选失败!');
    }

    public function deleteAddress() {
        if(isset($_GET['id'])) $this->address->deleteAddress() ? $this->redirect->succ(Tool::getPrevPage(),'收货地址删除成功!') : $this->redirect->error('系统出错，收货地址伤处失败!');
    }

    public function cancel() {
        $this->order->cancel() ? $this->redirect->succ(Tool::getPrevPage(),'订单已取消!') : $this->redirect->error('系统出错，订单取消失败!');
    }

    public function refund() {
        $this->order->refund() ? $this->redirect->succ(Tool::getPrevPage()) : $this->redirect->error('系统出错，申请退款失败!');
    }

    //重新下单
    public function reOrder() {
        if(($result = $this->order->reOrder()) == 1) {     //执行一次reOrder()方法后把结果保存到一个变量里，防止重复执行这个方法
            $this->redirect->succ(Tool::getPrevPage());
        } else if($result == 404) {
            $this->redirect->error('由于商品库存已经不足，重新下单失败!');
        } else {
            $this->redirect->error('系统出错，重新下单失败!');
        }
    }

    public function checkUserName() {
        $this->user->checkUserName();
    }

}

?>